package com.sqly.doc.ble.xyj;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.cazaea.sweetalert.SweetAlertDialog;
import com.clj.fastblexy.exception.BleException;
import com.clj.fastblexy.iscian.IscianFunction;
import com.clj.fastblexy.iscian.IscianService;
import com.clj.fastblexy.iscian.bean.MeasureResult;
import com.clj.fastblexy.iscian.bean.MeasuringData;
import com.clj.fastblexy.utils.HexUtil;
import com.sqly.doc.R;
import com.sqly.doc.base.MyBaseActivity;
import com.sqly.doc.menu.grjbxx.measure_xy_add;
import com.sqly.doc.menu.tnb.tnb;
import com.sqly.doc.util.SDCardUtil;

/**
 * 代码示范
 */
public class CodeMeasureActivity extends MyBaseActivity implements IscianService.BleMessageCallback {

    private IscianService mBluetoothService;
    private TextView textView;
    private Button btn_cancel,btn_start;
    private Button btn_stop;
    private ScrollView scrollView;
    private TextView tvResult, hrTextView, spTextView, dspTextView;
    private TextView tvMeasuring;
    private TextView title, uname;
    LinearLayout back;


    @Override
    protected void initUI() {
        setContentView(R.layout.activity_code_measure);
        initView();
        bindService();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    private void initView() {
        measure_xy_add.count +=1;
        textView = (TextView) findViewById(R.id.textView);
        tvResult = (TextView) findViewById(R.id.tv_result);
        hrTextView = (TextView) findViewById(R.id.hrTextView);
        spTextView = (TextView) findViewById(R.id.spTextView);
        dspTextView = (TextView) findViewById(R.id.dspTextView);
        tvMeasuring = (TextView) findViewById(R.id.tv_measuring);
        scrollView = (ScrollView) findViewById(R.id.scroll_container);
        btn_start = (Button)findViewById(R.id.btn_start);
        btn_cancel = (Button)findViewById(R.id.btn_cancel);
        btn_stop = (Button)findViewById(R.id.btn_stop);
        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        title.setText("血压测量");
        uname.setText("确定");
        uname.setVisibility(View.GONE);
        back = (LinearLayout) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SweetAlertDialog(CodeMeasureActivity.this, SweetAlertDialog.ERROR_TYPE)
                        .setTitleText("提示")
                        .setContentText("本次测定结果为：收缩压/舒张压("+ spTextView.getText().toString()+"/"+ dspTextView.getText().toString()+"，心率："+ hrTextView.getText()+")，确定选择本次测定结果吗?")
                        .setConfirmText("确定")
                        .setCancelText("取消")
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                sweetAlertDialog.dismiss();
                                Intent intent = new Intent(CodeMeasureActivity.this, measure_xy_add.class);
                                Bundle b = new Bundle();
                                b.putString("deviceVal", spTextView.getText().toString());
                                b.putString("deviceVal2", dspTextView.getText().toString());
                                b.putString("deviceVal3", hrTextView.getText().toString());
                                intent.putExtras(b);
                                setResult(0, intent);
                                finish();
                            }
                        })
                        .show();
            }
        });
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
                mBluetoothService.startMeasure();
                break;
            case R.id.btn_cancel:
                mBluetoothService.cancelMeasure();
                break;
            case R.id.btn_stop:
                mBluetoothService.closeConnect();
                finish();
                break;
        }
    }

    private void bindService() {
        Intent bindIntent = new Intent(this, IscianService.class);
        this.bindService(bindIntent, mFhrSCon, Context.BIND_AUTO_CREATE);
    }

    private void unbindService() {
        this.unbindService(mFhrSCon);
    }

    private ServiceConnection mFhrSCon = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBluetoothService = ((IscianService.BluetoothBinder) service).getService();
            mBluetoothService.setBleMessageCallback(CodeMeasureActivity.this);
//            去掉下方注释，便是自动测量血压
//            mBluetoothService.startMeasure();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mBluetoothService = null;
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBluetoothService != null)
            unbindService();
    }


    private void updateMsg(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.append(msg + "\n");
                scrollView.scrollTo(0, textView.getMeasuredHeight());
                SDCardUtil.CreatFileText(msg+"\n","新设备"+ measure_xy_add.count);
            }
        });
    }

    private static final String TAG = "CodeMeasureActivity";

    @Override
    public void onNotifyDataChanged(byte[] data) {
        String msg = HexUtil.encodeHexStr(data);
        Log.d(TAG, "onNotifyDataChanged: " + msg);
        updateMsg(msg);
    }

    private void sendTicking() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //开始测量
                mBluetoothService.writeCMD(IscianFunction.CMD_TICKING_NORMAL);
            }
        }, 200);
    }

    @Override
    public void onMeasuringDataUpdate(final MeasuringData data) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvMeasuring.setText(data.toString());
            }
        });
    }

    @Override
    public void onMeasureResultUpdate(final MeasureResult data) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvResult.setText(data.toString());
                hrTextView.setText(String.valueOf(data.HR+" "));
                spTextView.setText(String.valueOf(data.SP+" "));
                dspTextView.setText(String.valueOf(data.DSP+" "));
                uname.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void onFailure(BleException exception) {

    }

    @Override
    public void onCancel() {

    }
}
